<template>
  <el-dialog
    v-el-drag-dialog
    :title="!dataForm.id ? '新增' : '打款'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="100px">
      <el-form-item label="注册时间" prop="addTime">
        <span>{{dataForm.addTime}}</span>
        <!-- <el-input v-model="dataForm.addTime" placeholder="注册时间"></el-input> -->
      </el-form-item>
      <el-form-item label="运营商账号" prop="username">
        <!-- <el-input v-model="dataForm.username" placeholder="运营商账号"></el-input> -->
        <span>{{dataForm.username}}</span>
      </el-form-item>
      <el-form-item label="运营商名称" prop="trueName">
        <!-- <el-input v-model="dataForm.trueName" placeholder="运营商名称"></el-input> -->
        <span>{{dataForm.trueName}}</span>
      </el-form-item>
      <el-form-item label="运营商负责人" prop="realName">
        <!-- <el-input v-model="dataForm.realName" placeholder="运营商负责人"></el-input> -->
        <span>{{dataForm.realName}}</span>
      </el-form-item>
      <el-form-item label="会员类型" prop="userType">
        <span>{{dataForm.userType}}</span>
        <!-- <el-input v-model="dataForm.userType" placeholder="会员类型"></el-input> -->
      </el-form-item>
      <el-form-item label="推广余额" prop="commissionMoney">
        <span>{{dataForm.commissionMoney}}</span>
        <!-- <el-input v-model="dataForm.commissionMoney" placeholder="推广余额"></el-input> -->
      </el-form-item>
      <el-form-item label="已提现/元" prop="yesExtract">
        <span>{{dataForm.yesExtract}}</span>
        <!-- <el-input v-model="dataForm.yesExtract" placeholder="已提现/元"></el-input> -->
      </el-form-item>
      <el-form-item label="打款金额" prop="sendMoney">
        <el-input v-model="dataForm.sendMoney" placeholder="打款金额" style="width:100px" autofocus="true"></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()" :disabled="isDisabled">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        isDisabled: false,
        visible: false,
        dialogImageUrl: '',
        dialogVisible: false,
        dataForm: {
          id: 0,
          addTime: '',
          username: '',
          trueName: '',
          realName: '',
          userType: '',
          commissionMoney: '',
          yesExtract: '',
          sendMoney: '',
          userTypes: ''
        },
        dataRule: {
          sendMoney: [
            { required: true, message: '打款金额不能为空', trigger: 'blur' },
            // { pattern: /^[0-9]+[.]{1}[0-9]{2}$/, message: '打款金额为两位小数', trigger: 'blur' }
            { pattern: /^(0|[1-9]\d*)(\.\d{1,2})?$/, message: '数据不合法', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.isDisabled = false
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/user/finance/getFinanceById/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.id = data.data.id
                this.dataForm.addTime = data.data.addTime
                this.dataForm.username = data.data.username
                this.dataForm.trueName = data.data.trueName
                this.dataForm.realName = data.data.realName
                this.dataForm.userType = data.data.userType
                this.dataForm.commissionMoney = data.data.commissionMoney
                this.dataForm.yesExtract = data.data.yesExtract
                this.dataForm.userTypes = data.data.userTypes
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        if (this.dataForm.sendMoney > this.dataForm.commissionMoney) {
          this.$message.error('打款金额不能大于推广余额')
          return false
        }
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$confirm('确认打款吗？此操作不可撤回。')
              .then(_ => {
                this.isDisabled = true
                this.$http({
                  url: this.$http.adornUrl(`/user/finance/modifyMoneyById`),
                  method: 'post',
                  data: this.$http.adornData({
                    'id': this.dataForm.id || undefined,
                    'username': this.dataForm.username,
                    'trueName': this.dataForm.trueName,
                    'realName': this.dataForm.realName,
                    'userTypes': this.dataForm.userTypes,
                    'commissionMoney': this.dataForm.commissionMoney,
                    'yesExtract': this.dataForm.yesExtract,
                    'sendMoney': this.dataForm.sendMoney
                  })
                }).then(({data}) => {
                  if (data && data.code === 0) {
                    this.$message({
                      message: '操作成功',
                      type: 'success',
                      duration: 1500,
                      onClose: () => {
                        this.visible = false
                        this.$emit('refreshDataList')
                      }
                    })
                  } else {
                    this.$message.error(data.msg)
                  }
                })
              })
          }
        })
      }
    }
  }
</script>
